<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>link.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_editor_link.js.html">link.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A utility class for managing editable links.
<a name="line17"></a> *
<a name="line18"></a> * @author nicksantos@google.com (Nick Santos)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.editor.Link&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.array&#39;);
<a name="line24"></a>goog.require(&#39;goog.dom&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom.Range&#39;);
<a name="line27"></a>goog.require(&#39;goog.editor.BrowserFeature&#39;);
<a name="line28"></a>goog.require(&#39;goog.editor.Command&#39;);
<a name="line29"></a>goog.require(&#39;goog.editor.node&#39;);
<a name="line30"></a>goog.require(&#39;goog.editor.range&#39;);
<a name="line31"></a>goog.require(&#39;goog.string&#39;);
<a name="line32"></a>goog.require(&#39;goog.string.Unicode&#39;);
<a name="line33"></a>goog.require(&#39;goog.uri.utils&#39;);
<a name="line34"></a>goog.require(&#39;goog.uri.utils.ComponentIndex&#39;);
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>/**
<a name="line39"></a> * Wrap an editable link.
<a name="line40"></a> * @param {HTMLAnchorElement} anchor The anchor element.
<a name="line41"></a> * @param {boolean} isNew Whether this is a new link.
<a name="line42"></a> * @constructor
<a name="line43"></a> */
<a name="line44"></a>goog.editor.Link = function(anchor, isNew) {
<a name="line45"></a>  /**
<a name="line46"></a>   * The link DOM element.
<a name="line47"></a>   * @type {HTMLAnchorElement}
<a name="line48"></a>   * @private
<a name="line49"></a>   */
<a name="line50"></a>  this.anchor_ = anchor;
<a name="line51"></a>
<a name="line52"></a>  /**
<a name="line53"></a>   * Whether this link represents a link just added to the document.
<a name="line54"></a>   * @type {boolean}
<a name="line55"></a>   * @private
<a name="line56"></a>   */
<a name="line57"></a>  this.isNew_ = isNew;
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>  /**
<a name="line61"></a>   * Any extra anchors created by the browser from a selection in the same
<a name="line62"></a>   * operation that created the primary link
<a name="line63"></a>   * @type {!Array.&lt;HTMLAnchorElement&gt;}
<a name="line64"></a>   * @private
<a name="line65"></a>   */
<a name="line66"></a>  this.extraAnchors_ = [];
<a name="line67"></a>};
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>/**
<a name="line71"></a> * @return {HTMLAnchorElement} The anchor element.
<a name="line72"></a> */
<a name="line73"></a>goog.editor.Link.prototype.getAnchor = function() {
<a name="line74"></a>  return this.anchor_;
<a name="line75"></a>};
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/**
<a name="line79"></a> * @return {!Array.&lt;HTMLAnchorElement&gt;} The extra anchor elements, if any,
<a name="line80"></a> *     created by the browser from a selection.
<a name="line81"></a> */
<a name="line82"></a>goog.editor.Link.prototype.getExtraAnchors = function() {
<a name="line83"></a>  return this.extraAnchors_;
<a name="line84"></a>};
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * @return {string} The inner text for the anchor.
<a name="line89"></a> */
<a name="line90"></a>goog.editor.Link.prototype.getCurrentText = function() {
<a name="line91"></a>  if (!this.currentText_) {
<a name="line92"></a>    this.currentText_ = goog.dom.getRawTextContent(this.getAnchor());
<a name="line93"></a>  }
<a name="line94"></a>  return this.currentText_;
<a name="line95"></a>};
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * @return {boolean} Whether the link is new.
<a name="line100"></a> */
<a name="line101"></a>goog.editor.Link.prototype.isNew = function() {
<a name="line102"></a>  return this.isNew_;
<a name="line103"></a>};
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * Set the url without affecting the isNew() status of the link.
<a name="line108"></a> * @param {string} url A URL.
<a name="line109"></a> */
<a name="line110"></a>goog.editor.Link.prototype.initializeUrl = function(url) {
<a name="line111"></a>  this.getAnchor().href = url;
<a name="line112"></a>};
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * Removes the link, leaving its contents in the document.  Note that this
<a name="line117"></a> * object will no longer be usable/useful after this call.
<a name="line118"></a> */
<a name="line119"></a>goog.editor.Link.prototype.removeLink = function() {
<a name="line120"></a>  goog.dom.flattenElement(this.anchor_);
<a name="line121"></a>  this.anchor_ = null;
<a name="line122"></a>  while (this.extraAnchors_.length) {
<a name="line123"></a>    goog.dom.flattenElement(/** @type {Element} */(this.extraAnchors_.pop()));
<a name="line124"></a>  }
<a name="line125"></a>};
<a name="line126"></a>
<a name="line127"></a>
<a name="line128"></a>/**
<a name="line129"></a> * Change the link.
<a name="line130"></a> * @param {string} newText New text for the link. If the link contains all its
<a name="line131"></a> *     text in one descendent, newText will only replace the text in that
<a name="line132"></a> *     one node. Otherwise, we&#39;ll change the innerHTML of the whole
<a name="line133"></a> *     link to newText.
<a name="line134"></a> * @param {string} newUrl A new URL.
<a name="line135"></a> */
<a name="line136"></a>goog.editor.Link.prototype.setTextAndUrl = function(newText, newUrl) {
<a name="line137"></a>  var anchor = this.getAnchor();
<a name="line138"></a>  anchor.href = newUrl;
<a name="line139"></a>
<a name="line140"></a>  // If the text did not change, don&#39;t update link text.
<a name="line141"></a>  var currentText = this.getCurrentText();
<a name="line142"></a>  if (newText != currentText) {
<a name="line143"></a>    var leaf = goog.editor.node.getLeftMostLeaf(anchor);
<a name="line144"></a>    if (leaf.nodeType == goog.dom.NodeType.TEXT) {
<a name="line145"></a>      leaf = leaf.parentNode;
<a name="line146"></a>    }
<a name="line147"></a>
<a name="line148"></a>    if (goog.dom.getRawTextContent(leaf) != currentText) {
<a name="line149"></a>      leaf = anchor;
<a name="line150"></a>    }
<a name="line151"></a>
<a name="line152"></a>    goog.dom.removeChildren(leaf);
<a name="line153"></a>
<a name="line154"></a>    var domHelper = goog.dom.getDomHelper(leaf);
<a name="line155"></a>    goog.dom.appendChild(leaf, domHelper.createTextNode(newText));
<a name="line156"></a>
<a name="line157"></a>    // The text changed, so force getCurrentText to recompute.
<a name="line158"></a>    this.currentText_ = null;
<a name="line159"></a>  }
<a name="line160"></a>
<a name="line161"></a>  this.isNew_ = false;
<a name="line162"></a>};
<a name="line163"></a>
<a name="line164"></a>
<a name="line165"></a>/**
<a name="line166"></a> * Places the cursor to the right of the anchor.
<a name="line167"></a> * Note that this is different from goog.editor.range&#39;s placeCursorNextTo
<a name="line168"></a> * in that it specifically handles the placement of a cursor in browsers
<a name="line169"></a> * that trap you in links, by adding a space when necessary and placing the
<a name="line170"></a> * cursor after that space.
<a name="line171"></a> */
<a name="line172"></a>goog.editor.Link.prototype.placeCursorRightOf = function() {
<a name="line173"></a>  var anchor = this.getAnchor();
<a name="line174"></a>  // If the browser gets stuck in a link if we place the cursor next to it,
<a name="line175"></a>  // we&#39;ll place the cursor after a space instead.
<a name="line176"></a>  if (goog.editor.BrowserFeature.GETS_STUCK_IN_LINKS) {
<a name="line177"></a>    var spaceNode;
<a name="line178"></a>    var nextSibling = anchor.nextSibling;
<a name="line179"></a>
<a name="line180"></a>    // Check if there is already a space after the link.  Only handle the
<a name="line181"></a>    // simple case - the next node is a text node that starts with a space.
<a name="line182"></a>    if (nextSibling &amp;&amp;
<a name="line183"></a>        nextSibling.nodeType == goog.dom.NodeType.TEXT &amp;&amp;
<a name="line184"></a>        (goog.string.startsWith(nextSibling.data, goog.string.Unicode.NBSP) ||
<a name="line185"></a>         goog.string.startsWith(nextSibling.data, &#39; &#39;))) {
<a name="line186"></a>      spaceNode = nextSibling;
<a name="line187"></a>    } else {
<a name="line188"></a>      // If there isn&#39;t an obvious space to use, create one after the link.
<a name="line189"></a>      var dh = goog.dom.getDomHelper(anchor);
<a name="line190"></a>      spaceNode = dh.createTextNode(goog.string.Unicode.NBSP);
<a name="line191"></a>      goog.dom.insertSiblingAfter(spaceNode, anchor);
<a name="line192"></a>    }
<a name="line193"></a>
<a name="line194"></a>    // Move the selection after the space.
<a name="line195"></a>    var range = goog.dom.Range.createCaret(spaceNode, 1);
<a name="line196"></a>    range.select();
<a name="line197"></a>  } else {
<a name="line198"></a>    goog.editor.range.placeCursorNextTo(anchor, false);
<a name="line199"></a>  }
<a name="line200"></a>};
<a name="line201"></a>
<a name="line202"></a>
<a name="line203"></a>/**
<a name="line204"></a> * Updates the cursor position and link bubble for this link.
<a name="line205"></a> * @param {goog.editor.Field} field The field in which the link is created.
<a name="line206"></a> * @param {string} url The link url.
<a name="line207"></a> * @private
<a name="line208"></a> */
<a name="line209"></a>goog.editor.Link.prototype.updateLinkDisplay_ = function(field, url) {
<a name="line210"></a>  this.initializeUrl(url);
<a name="line211"></a>  this.placeCursorRightOf();
<a name="line212"></a>  field.execCommand(goog.editor.Command.UPDATE_LINK_BUBBLE);
<a name="line213"></a>};
<a name="line214"></a>
<a name="line215"></a>
<a name="line216"></a>/**
<a name="line217"></a> * @return {string?} The modified string for the link if the link
<a name="line218"></a> *     text appears to be a valid link. Returns null if this is not
<a name="line219"></a> *     a valid link address.
<a name="line220"></a> */
<a name="line221"></a>goog.editor.Link.prototype.getValidLinkFromText = function() {
<a name="line222"></a>  var text = this.getCurrentText();
<a name="line223"></a>  if (goog.editor.Link.isLikelyUrl(text)) {
<a name="line224"></a>    if (text.search(/:/) &lt; 0) {
<a name="line225"></a>      return &#39;http://&#39; + goog.string.trimLeft(text);
<a name="line226"></a>    }
<a name="line227"></a>    return text;
<a name="line228"></a>  } else if (goog.editor.Link.isLikelyEmailAddress(text)) {
<a name="line229"></a>    return &#39;mailto:&#39; + text;
<a name="line230"></a>  }
<a name="line231"></a>  return null;
<a name="line232"></a>};
<a name="line233"></a>
<a name="line234"></a>
<a name="line235"></a>/**
<a name="line236"></a> * After link creation, finish creating the link depending on the type
<a name="line237"></a> * of link being created.
<a name="line238"></a> * @param {goog.editor.Field} field The field where this link is being created.
<a name="line239"></a> */
<a name="line240"></a>goog.editor.Link.prototype.finishLinkCreation = function(field) {
<a name="line241"></a>  var linkFromText = this.getValidLinkFromText();
<a name="line242"></a>  if (linkFromText) {
<a name="line243"></a>    this.updateLinkDisplay_(field, linkFromText);
<a name="line244"></a>  } else {
<a name="line245"></a>    field.execCommand(goog.editor.Command.MODAL_LINK_EDITOR, this);
<a name="line246"></a>  }
<a name="line247"></a>};
<a name="line248"></a>
<a name="line249"></a>
<a name="line250"></a>/**
<a name="line251"></a> * Initialize a new link.
<a name="line252"></a> * @param {HTMLAnchorElement} anchor The anchor element.
<a name="line253"></a> * @param {string} url The initial URL.
<a name="line254"></a> * @param {string=} opt_target The target.
<a name="line255"></a> * @param {Array.&lt;HTMLAnchorElement&gt;=} opt_extraAnchors Extra anchors created
<a name="line256"></a> *     by the browser when parsing a selection.
<a name="line257"></a> * @return {goog.editor.Link} The link.
<a name="line258"></a> */
<a name="line259"></a>goog.editor.Link.createNewLink = function(anchor, url, opt_target,
<a name="line260"></a>    opt_extraAnchors) {
<a name="line261"></a>  var link = new goog.editor.Link(anchor, true);
<a name="line262"></a>  link.initializeUrl(url);
<a name="line263"></a>
<a name="line264"></a>  if (opt_target) {
<a name="line265"></a>    anchor.target = opt_target;
<a name="line266"></a>  }
<a name="line267"></a>  if (opt_extraAnchors) {
<a name="line268"></a>    link.extraAnchors_ = opt_extraAnchors;
<a name="line269"></a>  }
<a name="line270"></a>
<a name="line271"></a>  return link;
<a name="line272"></a>};
<a name="line273"></a>
<a name="line274"></a>
<a name="line275"></a>/**
<a name="line276"></a> * Returns true if str could be a URL, false otherwise
<a name="line277"></a> *
<a name="line278"></a> * Ex: TR_Util.isLikelyUrl_(&quot;http://www.google.com&quot;) == true
<a name="line279"></a> *     TR_Util.isLikelyUrl_(&quot;www.google.com&quot;) == true
<a name="line280"></a> *
<a name="line281"></a> * @param {string} str String to check if it looks like a URL.
<a name="line282"></a> * @return {boolean} Whether str could be a URL.
<a name="line283"></a> */
<a name="line284"></a>goog.editor.Link.isLikelyUrl = function(str) {
<a name="line285"></a>  // Whitespace means this isn&#39;t a domain.
<a name="line286"></a>  if (/\s/.test(str)) {
<a name="line287"></a>    return false;
<a name="line288"></a>  }
<a name="line289"></a>
<a name="line290"></a>  if (goog.editor.Link.isLikelyEmailAddress(str)) {
<a name="line291"></a>    return false;
<a name="line292"></a>  }
<a name="line293"></a>
<a name="line294"></a>  // Add a scheme if the url doesn&#39;t have one - this helps the parser.
<a name="line295"></a>  var addedScheme = false;
<a name="line296"></a>  if (!/^[^:\/?#.]+:/.test(str)) {
<a name="line297"></a>    str = &#39;http://&#39; + str;
<a name="line298"></a>    addedScheme = true;
<a name="line299"></a>  }
<a name="line300"></a>
<a name="line301"></a>  // Parse the domain.
<a name="line302"></a>  var parts = goog.uri.utils.split(str);
<a name="line303"></a>
<a name="line304"></a>  // Relax the rules for special schemes.
<a name="line305"></a>  var scheme = parts[goog.uri.utils.ComponentIndex.SCHEME];
<a name="line306"></a>  if (goog.array.indexOf([&#39;mailto&#39;, &#39;aim&#39;], scheme) != -1) {
<a name="line307"></a>    return true;
<a name="line308"></a>  }
<a name="line309"></a>
<a name="line310"></a>  // Require domains to contain a &#39;.&#39;, unless the domain is fully qualified and
<a name="line311"></a>  // forbids domains from containing invalid characters.
<a name="line312"></a>  var domain = parts[goog.uri.utils.ComponentIndex.DOMAIN];
<a name="line313"></a>  if (!domain || (addedScheme &amp;&amp; domain.indexOf(&#39;.&#39;) == -1) ||
<a name="line314"></a>      (/[^\w\d\-\u0100-\uffff.%]/.test(domain))) {
<a name="line315"></a>    return false;
<a name="line316"></a>  }
<a name="line317"></a>
<a name="line318"></a>  // Require http and ftp paths to start with &#39;/&#39;.
<a name="line319"></a>  var path = parts[goog.uri.utils.ComponentIndex.PATH];
<a name="line320"></a>  return !path || path.indexOf(&#39;/&#39;) == 0;
<a name="line321"></a>};
<a name="line322"></a>
<a name="line323"></a>
<a name="line324"></a>/**
<a name="line325"></a> * Regular expression that matches strings that could be an email address.
<a name="line326"></a> * @type {RegExp}
<a name="line327"></a> * @private
<a name="line328"></a> */
<a name="line329"></a>goog.editor.Link.LIKELY_EMAIL_ADDRESS_ = new RegExp(
<a name="line330"></a>    &#39;^&#39; +                     // Test from start of string
<a name="line331"></a>    &#39;[\\w-]+(\\.[\\w-]+)*&#39; +  // Dot-delimited alphanumerics and dashes (name)
<a name="line332"></a>    &#39;\\@&#39; +                   // @
<a name="line333"></a>    &#39;([\\w-]+\\.)+&#39; +         // Alphanumerics, dashes and dots (domain)
<a name="line334"></a>    &#39;(\\d+|\\w\\w+)$&#39;,        // Domain ends in at least one number or 2 letters
<a name="line335"></a>    &#39;i&#39;);
<a name="line336"></a>
<a name="line337"></a>
<a name="line338"></a>/**
<a name="line339"></a> * Returns true if str could be an email address, false otherwise
<a name="line340"></a> *
<a name="line341"></a> * Ex: goog.editor.Link.isLikelyEmailAddress_(&quot;some word&quot;) == false
<a name="line342"></a> *     goog.editor.Link.isLikelyEmailAddress_(&quot;foo@foo.com&quot;) == true
<a name="line343"></a> *
<a name="line344"></a> * @param {string} str String to test for being email address.
<a name="line345"></a> * @return {boolean} Whether &quot;str&quot; looks like an email address.
<a name="line346"></a> */
<a name="line347"></a>goog.editor.Link.isLikelyEmailAddress = function(str) {
<a name="line348"></a>  return goog.editor.Link.LIKELY_EMAIL_ADDRESS_.test(str);
<a name="line349"></a>};
<a name="line350"></a>
<a name="line351"></a>
<a name="line352"></a>/**
<a name="line353"></a> * Determines whether or not a url is an email link.
<a name="line354"></a> * @param {string} url A url.
<a name="line355"></a> * @return {boolean} Whether the url is a mailto link.
<a name="line356"></a> */
<a name="line357"></a>goog.editor.Link.isMailto = function(url) {
<a name="line358"></a>  return !!url &amp;&amp; goog.string.startsWith(url, &#39;mailto:&#39;);
<a name="line359"></a>};
</pre>


</body>
</html>
